#include <stdio.h>
#include "Background.h"

Background::Background(TextureManager * textureManager, char * backgroundFile, ParallaxLayer * parallaxLayers, int numLayers)
{
	this->textureManager = textureManager;
	this->parallaxLayers = parallaxLayers;
	this->numLayers = numLayers;
	
	if(this->textureManager)
	{
		if(!this->textureManager->LoadTexture(backgroundFile))
		{
			printf("Failed to load %s!\n", backgroundFile);
		}
		else
		{
			backgroundIndex = textureManager->TextureIndex(backgroundFile);
		}
	}
}
Background::~Background()
{
	//for(int i = 0; i < numLayers; i++)
	//	delete(&parallaxLayers[i]);
	if(parallaxLayers)
		delete(parallaxLayers);
	parallaxLayers = NULL;
}
void Background::Think(Vector2D cameraTranslation)
{
	for(int i=0; i < numLayers; i++)
		parallaxLayers[i].Think(cameraTranslation);
}
void Background::Draw()
{		
	glEnable(GL_TEXTURE_2D);
	//glAlphaFunc( GL_GREATER, 0.5 );
  	glEnable( GL_ALPHA_TEST );
  	glEnable( GL_BLEND);
  	
  	glPushMatrix();
	glLoadIdentity();
	glTranslatef(0, 0, -0.5);
	
	for(int i=0; i < numLayers; i++)
		parallaxLayers[i].Draw();
		
	if(textureManager)
		textureManager->EnableTexture(backgroundIndex);
	glDisable(GL_BLEND);
	glDisable( GL_ALPHA_TEST );
	glEnable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
	glTexCoord2f(0,0.75);
	glVertex2f(0,1);
	glTexCoord2f(1,0.75);
	glVertex2f(1,1);
	glTexCoord2f(1,0);
	glVertex2f(1,0);
	glTexCoord2f(0,0);
	glVertex2f(0,0);

	glEnd();
		
	glDisable(GL_TEXTURE_2D);
	glPopMatrix();
}